<template>
  <el-card class="swiper-container">
    <template #header>
      <el-button type="primary" size="default ">
        <el-icon><plus /></el-icon>
        增加
      </el-button>
      <el-popconfirm title="确定删除吗？" @confirm="handleDelete">
        <template #reference>
          <el-button type="danger" size="default">
            <el-icon><plus /></el-icon>
            批量删除
          </el-button>
        </template>
      </el-popconfirm>
    </template>
    <el-table
    v-loading="loading"
    :data="tableData"
    ref="multipleTable"
    tooltip-effect="dark"
    style="width: 100%;"
     @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55"
      ></el-table-column>
      <el-table-column
        label="轮播图"
        width="200"
      >
        <template #default="scope">
          <img style="width: 150px; height: 150px;" :src="scope.row.carouselUrl" alt="轮播图"/>
        </template>
      </el-table-column>
      <el-table-column 
        label="跳转链接"
      >
        <template #default="scope">
          <a target="_blank" style="text-decoration: none;" :href="scope.row.redirectUrl">{{scope.row.redirectUrl}}</a>
        </template>
      </el-table-column>
      <el-table-column
        prop="carouselRank"
        label="排序值"
        width="120"
      ></el-table-column>
      <el-table-column 
        prop="createTime"
        label="添加时间"
        width="200"
      ></el-table-column>
      <el-table-column
        label="操作"
        width="100"
      >
        <template #default="scope">
          <a style="cursor: pointer;margin-right: 10px;color: blue;">修改</a>
          <el-popconfirm
            title="确定删除么？"
            @confirm="handleDeleteOne(scope.row.carouselId)"
          >
            <template #reference>
              <a style="cursor: pointer;color: blue;">删除</a>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--总数超过一页，再展示分页器-->
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :page-size="pageSize"
      :current-page="currentPage"
      @current-change="changePage"
      hide-on-single-page
      class="pagination-sty"
    />
  </el-card>
</template>

<script>
import { Plus } from "@element-plus/icons-vue";
import { reactive, ref, toRefs, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import axios from '@/utils/axios'
export default {
  name: "swiper",
  components: { Plus },
  setup(){
    const multipleTable = ref(null);

    const state = reactive({
      loading: false,
      tableData: [], //列表数据
      multipleSelection: [], // 选中项
      total: 0, // 总条数
      currentPage: 1,
      pageSize: 10,
      type: 'add', // 操作类型
    })

    onMounted(()=>{
      getCarousels();
    })

    const getCarousels = ()=> {
      state.loading = true;
      axios.get('/carousels', {
        params: {
          pageNumber: state.currentPage,
          pageSize: state.pageSize
        }
      }).then(res=>{
        state.tableData = res.list
        state.total = res.totalCount
        state.currentPage = res.currentPage
        state.loading = false
      })
    }

    const handleSelectionChange = (val) => {
      state.multipleSelection = val
    }
     // 批量删除
    const handleDelete = () => {
      if (!state.multipleSelection.length) {
        ElMessage.error('请选择项')
        return
      }
      axios.delete('/carousels', {
        data: {
          ids: state.multipleSelection.map(i => i.carouselId)
        }
      }).then(() => {
        ElMessage.success('删除成功')
        getCarousels()
      })
    }
    // 单个删除
    const handleDeleteOne = (id) => {
      axios.delete('/carousels', {
        data: {
          ids: [id]
        }
      }).then(() => {
        ElMessage.success('删除成功')
        getCarousels()
      })
    }

     const changePage = (val) => {
      state.currentPage = val
      getCarousels()
    }

    return {
      ...toRefs(state),
      multipleTable,
      handleSelectionChange,
      handleDelete,
      handleDeleteOne,
      changePage
    }
  }
};
</script>

<style>
.swiper-container {
    min-height: 100%;
  }
  .el-card.is-always-shadow {
    min-height: 100%!important;
  }

  .pagination-sty {
    text-align: center;
    margin-top: 30px;
  }
</style>